import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import { Tag, TagParam } from '@/api/employ/tags/model';

/**
 * 分页查询
 */
export async function pageTags(params: TagParam) {
  const res = await request.get<ApiResult<PageResult<Tag>>>(
    '/employ/tags/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询列表
 */
export async function listTags(params?: TagParam) {
  const res = await request.get<ApiResult<Tag[]>>('/employ/tags', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加
 */
export async function addTag(data: Tag) {
  const res = await request.post<ApiResult<unknown>>('/employ/tags', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改
 */
export async function updateTag(data: Tag) {
  const res = await request.put<ApiResult<unknown>>('/employ/tags', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function removeTag(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/employ/tags/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除
 */
export async function removeTagBatch(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/employ/tags/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
